package com.kxf.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.text.TextUtils;

public class ProcessUtil {
    private ProcessUtil(){

    }

    //判断当前是否在主进程
    public static boolean isMainProcess(Context context) {
        return TextUtils.equals(getCurrentProcessName(context), context.getPackageName());
    }

    private static String getCurrentProcessName(Context context) {
        int pid = android.os.Process.myPid();
        ActivityManager mActivityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager
                .getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }
}
